package com.ashye.kotlinversion

import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView

/**
 * Created by zhangwei on 2017/6/28.
 */
class FooterBindingRecyclerAdapter<T>(var lid: Int, var vid: Int) : RecyclerView.Adapter<FooterBindingRecyclerAdapter.Companion.BindingViewHolder>(),
                                                                View.OnClickListener {

    internal enum class ViewType {
        TYPE_ITEM,
        TYPE_FOOTER
    }


    private var showFooter = true
    private var footerStatus = 0


    private val listData = mutableListOf<T>()
    private var onItemClicked: ((view: View, position: Int, item: T) -> Unit)? = null

    fun setOnItemClicked(listener: (v: View, i: Int, item: T) -> Unit) {
        onItemClicked = listener
    }

    override fun onClick(p0: View?) {
        onItemClicked?.apply {
            p0?.apply {
                if (tag is Int) {
                    val pos = tag as Int
                    invoke(this, pos, listData[pos])
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (footerCount > 0 && position == itemCount - 1) {
            return ViewType.TYPE_FOOTER.ordinal
        } else {
            return ViewType.TYPE_ITEM.ordinal
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder {
        if (viewType == ViewType.TYPE_FOOTER.ordinal) {
            val tv = TextView(parent.context)
            val param = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            param.height = 150
            tv.layoutParams = param
            tv.gravity = Gravity.CENTER
            tv.setTextColor(tv.context.resources.getColor(android.R.color.secondary_text_dark))
            return BindingViewHolder(tv, false)
        } else {
            val vh = BindingViewHolder(LayoutInflater.from(parent.context).inflate(lid, parent, false))
            vh.itemView.setOnClickListener(this)
            return vh
        }
    }

    override fun onBindViewHolder(holder: BindingViewHolder?, position: Int) {
        holder?.apply {

            if (getItemViewType(position) == ViewType.TYPE_FOOTER.ordinal) {
                if (!showFooter) {
                    return
                }
                (itemView as TextView).text = footerString
            } else if (getItemViewType(position) == ViewType.TYPE_ITEM.ordinal) {
                binding?.apply {
                    setVariable(vid, listData[position])
                    executePendingBindings()
                    itemView.tag = position
                }
            }
        }
    }

    private val footerString: String
        get() {
            when (footerStatus) {
                0 -> return "加载更多"
                1 -> return "正在加载"
                2 -> return "全部加载"
                else -> return ""
            }
        }


    private val footerCount: Int
        get() = if (showFooter) 1 else 0


    fun hideFooter() {
        if (showFooter) {
            showFooter = false
            notifyItemRemoved(itemCount)
        }
    }

    fun loading(loading: Boolean) {
        footerStatus = if (loading) 1 else 0
        if (showFooter) {
            notifyItemChanged(itemCount - 1)
        } else {
            showFooter = true
            notifyItemInserted(itemCount)
        }
    }

    fun loadAll() {
        footerStatus = 2
        if (showFooter) {
            notifyItemChanged(itemCount - 1)
        } else {
            showFooter = true
            notifyItemInserted(itemCount)
        }
    }

    override fun getItemCount() = listData.size + footerCount

    override fun getItemId(position: Int) = if (footerCount > 0) super.getItemId(position) -1 else super.getItemId(position)

    fun addData(list: List<T>) {
        val size = listData.size
        listData.addAll(list)
        notifyItemRangeInserted(size, list.size)
    }

    fun addData(item: T) {
        listData.add(item)
        notifyItemInserted(listData.size -1)
    }

    fun setData(list: List<T>) {
        listData.clear()
        addData(list)
    }




    companion object {
        class BindingViewHolder(view: View, bind: Boolean = true) : RecyclerView.ViewHolder(view) {
            val binding: ViewDataBinding?

            init {
                if (bind) {
                    binding = DataBindingUtil.bind(view)
                }else {
                    binding = null
                }
            }
        }
    }
}
